<?php
if(!defined('CORE_ROOT')) {
	header("location:index.php");
	exit();
}
require CORE_ROOT.'include/install.func.php';
$nodb = 1;
require CORE_ROOT.'include/admin.inc.php';
if(substr_count($_SERVER['SCRIPT_NAME'], '/') < 2) {
	aexit("<a href='http://www.akcms.com/manual/197.htm'>{$lan['rootinstallerror']}</a>");
}
if(ifinstalled()) {
	adminmsg($lan['reinstallerror'], 'http://www.akcms.com/manual/198.htm', 999, 1);
}
if(!empty($get_language)) $language = $get_language;
if(!empty($post_language)) $language = $post_language;
if(!empty($post_charset)) $charset = $post_charset;
$array_files = array(
	'..',
	'cache',
	'cache/tasks',
	'cache/templates',
	'cache/foretemplates',
	'configs',
	'configs/templates',
	'configs/templates/ak',
	'logs',
	'data',
	'index'
);
$message = '';
foreach($array_files as $file) {
	$_file = AK_ROOT.$file;
	$result = true;
	if(file_exists($_file)) {
		if(!is_writable($_file)) $result = false;
	} else {
		$result = ak_mkdir($_file);
	}
	if($result === false) $message .= '"'.$file.'"'.$lan['isunwritable'].'<br>';
}
if(!empty($message)) aexit('<a href="http://www.akcms.com/manual/196.htm" target="_blank">'.$message.'</a>');
if(isset($post_installsubmit) || isset($get_forcereinstall)) {
	if(isset($post_installsubmit)) {
		if(!preg_match('/^[0-9a-zA-Z]+$/i', $post_tablepre)) adminmsg($lan['tablepreerror'], 'back', 3, 1);
		createconfig($_POST);
	}
	require(AK_ROOT.'configs/config.inc.php');
	$db = db();
	createakcmstables($dbname);
	installinitialdata();
	setsetting('dataversion', $sysedition);
	setinstalled();
	updatecache();
	createfore();
	adminmsg($lan['installsuccess'], AK_URL.'index.php?file=theme');
} elseif(!isset($_GET['dbtype'])) {
	if(isset($_GET['agree'])) {
		$drivers = array(
			'mysql' => 'MySQL'."({$lan['currenthost']}{$lan['nosupport']})",
			'sqlite' => 'SQLite'."({$lan['currenthost']}{$lan['nosupport']})",
			'pdo:mysql' => 'pdo:mysql'."({$lan['currenthost']}{$lan['nosupport']})",
			'pdo:sqlite' => 'pdo:sqlite'."({$lan['currenthost']}{$lan['nosupport']})"
		);
		if(function_exists('mysql_connect')) $drivers['mysql'] = 'MySQL';
		if(function_exists('sqlite_open')) $drivers['sqlite'] = 'SQLite'.substr(sqlite_libversion(), 0, 1);
		if(function_exists('pdo_drivers')) {
			$pdodrivers = pdo_drivers();
			foreach($pdodrivers as $d) {
				$drivers['pdo:'.$d] = 'pdo:'.$d;
			}
		}
		$str='';
		foreach($drivers as $key => $value) {
			if(strpos($value,'(') === false){
				$str .= "<option value=".$key.">".$value.'</option>';
			} else {
				$str .= '<optgroup label='.$value.'></optgroup>';
			}
		}
		displaytemplate('choosedb.htm', array('drivers' => $str));
	} else {
		if(!isset($get_language)) $get_language = 'chinese';
		$language = $get_language;
		displaytemplate('license.htm');
		aexit();
	}
} else {
	$variables = array();
	$variables['servertime'] = date('Y-m-d H:i:s');
	$languagecharset = array(
		'key' => 'gbk',
		'value' => 'GBK'
	);
	if(file_exists(AK_ROOT."configs/config.inc.php")) {
		include(AK_ROOT."configs/config.inc.php");
		$variables['charset'] = $charset;
	}
	$variables['languagecharset'] = $languagecharset;
	if(strpos($get_dbtype, 'mysql') !== false) {
		displaytemplate('install_mysql.htm', $variables);
	} else {
		$variables['dbname'] = 'data/'.random(6).'.db.php';
		displaytemplate('install_sqlite.htm', $variables);
	}
}
runinfo();
aexit();
?>